package com.oocl.ita.linstner.loginframe;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JTextField;

import com.oocl.ita.qqclientt.QQClient;
import com.oocl.ita.sakura.client.Client;
import com.oocl.ita.sakura.protocol.Protocol;
import com.oocl.ita.ui.ChatFrame;
import com.oocl.ita.ui.LoginFrame;

public class LoginButtonActionListener implements ActionListener {
	private JTextField jtf;
	private LoginFrame loginFrame;
	private String account;

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;		
	}

	public LoginButtonActionListener(JTextField jtf, LoginFrame loginFrame) {
			this.jtf = jtf;
			this.loginFrame = loginFrame;
	}

	public void actionPerformed(ActionEvent e) {
		/*
		 * 1. 点击按钮， 获取输入的account 
		 * 2. 封装Client类
		 * 2.5 建立和Server的链接 获取oos
		 * 3. 封装login Protocol， oos发送
		 *  4，跳转到主界面
		 *  5. 隐藏登录界面 调用dispose方法
		 */
		// debug
		System.out.println("LoginButton : " + QQClient.getClient());
		
//		try {
//			SwingUtilities.invokeAndWait(new Runnable() {
//				@Override
//				public void run() {
					new ChatFrame().setVisible(true);	
//				}
//			});
//		} catch (InvocationTargetException | InterruptedException e2) {
//			// TODO Auto-generated catch block
//			e2.printStackTrace();
//		}
		
		this.account = jtf.getText();
		System.out.println(this.account);
		Client client=new Client(this.account);
		QQClient.setClient(client);
		Protocol loginProtocol=new Protocol("login");
		loginProtocol.encapsulate("from",client);
		System.out.println("LoginButtonActionListener "  + QQClient.getClient() + " " + loginProtocol);
		try {
			QQClient.getOos().writeObject(loginProtocol);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		// 隐藏本窗口 
		loginFrame.dispose();
		
	}

}
